//
//  AddRepaymentViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/9.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class AddRepaymentViewController: KMViewController {
    
    fileprivate let repaymentView = AddRepaymentView()
    
    fileprivate var dayNumber = "1" //还款日期
    fileprivate var dateNumber = "1" //提前天数
    
    var addBlock : (()->())?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = addReminder
        addSubView()
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        repaymentView.delegate = self
        self.view.addSubview(repaymentView)
        
        repaymentView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }

}

extension AddRepaymentViewController : AddRepaymentViewDelegate {
    
    //MARK: 选择操作类型
    func selectOperte(index: Int) {
        
        switch index {
        case 1://选择银行
            
            let dataArray = [BankofChina,Constructionbank,ICBC,ABC,CITICBank,BankofCommunications,GuangfaBank,EverbrightBank,HSBCBank,MinshengBank,PufaBank,PostalBank,ChinaMerchantsBank,IndustrialBank,HenanRuralCredit,HSBC,BankofEastAsia,HuishangBank,ShanghaiRuralCommercialBank,GuangzhouBank,BankofBeijing,CitiBank,BaoshangBank,HangzhouBank,ShanghaiBank]
            showPickView(dataArray: dataArray, selectText: self.repaymentView.bankView.text) {[weak self] (text) in
                self?.repaymentView.bankView.text = text
            }
        case 2://提前天数
            let dataArray = ["1","2","3","4","5","6","7"]
            showPickView(dataArray: dataArray, selectText: self.dayNumber) {[weak self] (text) in
                self?.repaymentView.dayView.text = text + days
                self?.dayNumber = text
            }
        case 3://还款日期
            let dataArray = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]
            showPickView(dataArray: dataArray, selectText: self.dateNumber) {[weak self] (text) in
                self?.repaymentView.dateView.text = text + number
                self?.dateNumber = text
            }
        case 4://添加
            addRepayment()
            
        default:
            break
        }
    }
    
    //MARK: 显示选择框
    fileprivate func showPickView(dataArray:[String],selectText:String,block:@escaping (String)->()) {
        
        let selectTypeVC = SelectTypeViewController.init(dataArray: dataArray, selectText: selectText)
        selectTypeVC.modalPresentationStyle = .overCurrentContext
        selectTypeVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        selectTypeVC.textBlock = { (text) in
            block(text)
        }
        self.present(selectTypeVC, animated: false, completion: nil)
    }
    
    //MARK: 添加提醒
    fileprivate func addRepayment() {
        
        if (repaymentView.numberView.textView.text! as NSString).length != 4 {
            KMTool.showText(_text: inputBankCardNumberOfFour)
            return
        }
        
        if repaymentView.phoneView.textView.text == "" {
            KMTool.showText(_text: inputPhoneNumebr)
            return
        }
        
        
        var tixing = repaymentView.numberView.textView.text! + ","
        tixing += repaymentView.bankView.text + ","
        tixing += self.dateNumber + ","
        tixing += self.dayNumber + ","
        tixing += repaymentView.phoneView.textView.text! + ","
        tixing += repaymentView.remarksView.textView.text!
        
        KMTool.waiting()
        KMTool.requestMethod(.post, URLString: url_addRepayment, parameters: ["user_name":KMTool.stringForEnumKey(.phone) ?? "","tixing":tixing], success: { (value, json) in
            KMTool.stop()
            
            if json["status"].stringValue == "0" {
                self.addBlock!()
                self.navigationController?.popViewController(animated: true)
            }
            
            KMTool.showText(_text: json["data"].stringValue)
            
        }) {
            KMTool.stop()
        }
        
    }
}
